﻿using System;
using fastCSharp.openApi.weixin;
using fastCSharp;

namespace diantou.web.weixin
{
    /// <summary>
    /// 微信支付
    /// </summary>
    [fastCSharp.code.cSharp.webCall(IsPool = true)]
    partial class pay : xmlHandle<pay>
    {
        /// <summary>
        /// 扫码支付
        /// </summary>
        /// <param name="query">扫码支付回调请求参数</param>
        [fastCSharp.code.cSharp.webCall(IsSerializeBox = true)]
        public unsafe void qrCode(qrCodePayQuery query)
        {
            if (query == null)
            {
                fastCSharp.openApi.weixin.config.PayLog.Add("xml解析失败 " + form.Text, new System.Diagnostics.StackFrame(), false);
                serverError500();
            }
            else
            {
                fastCSharp.openApi.weixin.config.PayLog.Add("xml " + form.Text, new System.Diagnostics.StackFrame(), false);
                diantou.dataAccess.tcpCall.weixin.qrCodePay(query, callback<qrCodePayResult>);
            }
        }
        /// <summary>
        /// 支付系统完成回调
        /// </summary>
        /// <param name="query">扫码支付完成回调通知请求参数</param>
        [fastCSharp.code.cSharp.webCall(IsSerializeBox = true)]
        public unsafe void notify(payNotify notify)
        {
            if (notify == null)
            {
                fastCSharp.openApi.weixin.config.PayLog.Add("xml解析失败 " + form.Text, new System.Diagnostics.StackFrame(), false);
                serverError500();
            }
            else
            {
                fastCSharp.openApi.weixin.config.PayLog.Add("xml " + form.Text, new System.Diagnostics.StackFrame(), false);
                diantou.dataAccess.tcpCall.weixin.payNotify(notify, form.Text, callback<returnCode>);
            }
        }
        /// <summary>
        /// 回调返回
        /// </summary>
        /// <param name="value"></param>
        private unsafe void callback<valueType>(fastCSharp.net.returnValue<valueType> value)
        {
            if (value.Type == fastCSharp.net.returnValue.type.Success)
            {
                createResponse();
                try
                {
                    response(value.Value.ToXml(diantou.dataAccess.weixin.XmlSerializeConfig));
                    return;
                }
                catch (Exception error)
                {
                    fastCSharp.log.Default.Add(error, null, false);
                }
                finally { responseEnd(); }
            }
            fastCSharp.log.Default.Add("远程调用失败", new System.Diagnostics.StackFrame(), false);
            serverError500();
        }
    }
}
